#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <cstring>
#include <cstdlib>
#include <arpa/inet.h>

using namespace std;

#define SERVER_PORT 9999
#define BUFFER_SIZE 128

int main()
{
    /* 创建套接字的句柄 */
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd == -1)
    {
        perror("socket error:");
        exit(-1);
    }

    struct sockaddr_in serverAddr;
    memset(&serverAddr, 0, sizeof(serverAddr));
    /* IPV4 */
    serverAddr.sin_family = AF_INET;
    /* 设置端口 */
    serverAddr.sin_port = htons(SERVER_PORT);
    /* 设置服务器的ip */
    const char * serverIp = "192.168.152.128";
    /*  */
    inet_pton(AF_INET, serverIp, &(serverAddr.sin_addr.s_addr));

    /* 连接服务器 */
    int ret = connect(sockfd, reinterpret_cast<struct sockaddr*>(&serverAddr), sizeof(serverAddr));
    if(ret != 0)
    {
        perror("connect error:");
        exit(-1);
    }
    cout << "服务器连接成功" << endl;

    char sendBuffer[BUFFER_SIZE] = { 0 };
    while(1)
    {
        cout << " 请输入你的信息：" << endl;
        scanf("%s", sendBuffer);
        sendto(sockfd, sendBuffer, strlen(sendBuffer), 0, (const struct sockaddr*)&serverAddr, sizeof(serverAddr));
    }

    return 0;
}